/**
 * State数据模型
 */
export interface Action {
    type: string;
    payload?: any;
}


/**
 * Reducer函数
 */
export interface Reducer<T> {
    (state: T, action: Action): T;
}


// store负责调用reducer，然后保存state
/**
 * 在每一个应用中都只有一个Store和一个顶层Reducer
 */
export class Store<T> {
    private _state: T;

    constructor(private reducer: Reducer<T>, initializeState: T) {
        this._state = initializeState;
    }

    getState(): T {
        return this._state;
    }

    /**
     * dispatch函数不反悔任何值，它只负责更新state
     * @param action 
     */
    dispatch(action: Action) {
        this._state = this.reducer(this._state, action);
    }
}
